package org.jeecg.modules.common.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.common.entity.*;
import org.jeecg.modules.common.service.ICamelliaDeviceService;
import org.jeecg.modules.common.service.ICamelliaFactoryService;
import org.jeecg.modules.env.entity.CamelliaEnvWarning;
import org.jeecg.modules.env.service.ICamelliaEnvWarningService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * @Classname CamelliaStatisticsController
 * @Description TODO
 * @Date 2023/1/30 17:03
 * @Created by 10835
 */
@Api(tags="数据统计")
@RestController
@RequestMapping("/common/camelliaStatistics")
@Slf4j
public class CamelliaStatisticsController extends JeecgController<CamelliaFactory, ICamelliaFactoryService> {

    @Resource
    private ICamelliaDeviceService camelliaDeviceService;
    @Resource
    private ICamelliaEnvWarningService camelliaEnvWarningService;
    /**
     * 设备数量数据统计
     * @param
     * @return
     */
    @ApiOperation(value="设备统计数据查询", notes="设备统计数据查询")
    @GetMapping(value = "/device")
    public Result<DeviceStatistical> getDeviceStatistical(@RequestParam(name="camelliaSysCode") String camelliaSysCode) {
        DeviceStatistical deviceStatistical = new DeviceStatistical();
        QueryWrapper<CamelliaDevice> deviceBaseWrapper = new QueryWrapper<CamelliaDevice>().eq("camellia_sys_code",camelliaSysCode);
        QueryWrapper<CamelliaDevice> deviceOnLineWrapper = new QueryWrapper<CamelliaDevice>().eq("device_online_status", 1).eq("camellia_sys_code",camelliaSysCode);
        QueryWrapper<CamelliaDevice> deviceEnabledWrapper = new QueryWrapper<CamelliaDevice>().eq("device_enabled", 1).eq("camellia_sys_code",camelliaSysCode);
        deviceStatistical.setDeviceAllCount(camelliaDeviceService.count(deviceBaseWrapper));
        deviceStatistical.setDeviceOnlineCount(camelliaDeviceService.count(deviceOnLineWrapper));
        deviceStatistical.setDeviceEnabledCount(camelliaDeviceService.count(deviceEnabledWrapper));
        return Result.OK(deviceStatistical);
    }


    /**
     * 设备数量数据统计
     * @param
     * @return
     */
    @ApiOperation(value="报警统计数据查询", notes="报警统计数据查询")
    @GetMapping(value = "/warning")
    public Result<DeviceWarningStatistical> getWarningStatistical(@RequestParam(name="camelliaSysCode") String camelliaSysCode) {
        LocalDateTime localDateTime = LocalDateTime.now();
        DeviceWarningStatistical deviceWarningStatistical = new DeviceWarningStatistical();
        QueryWrapper<CamelliaEnvWarning> dayWrapper = new QueryWrapper<CamelliaEnvWarning>().ge("warning_date_time",LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toLocalDate()).eq("system_code",camelliaSysCode);;
        QueryWrapper<CamelliaEnvWarning> weekWrapper = new QueryWrapper<CamelliaEnvWarning>().ge("warning_date_time", localDateTime.minusDays(7).toLocalDate()).eq("system_code",camelliaSysCode);
        QueryWrapper<CamelliaEnvWarning> monthWrapper = new QueryWrapper<CamelliaEnvWarning>().ge("warning_date_time", localDateTime.minusDays(30).toLocalDate()).eq("system_code",camelliaSysCode);
        deviceWarningStatistical.setTodayWarningCount(camelliaEnvWarningService.count(dayWrapper));
        deviceWarningStatistical.setWeekWarningCount(camelliaEnvWarningService.count(weekWrapper));
        deviceWarningStatistical.setMonthWarningCount(camelliaEnvWarningService.count(monthWrapper));
        return Result.OK(deviceWarningStatistical);
    }

    @ApiOperation(value="报警记录查询", notes="报警记录查询")
    @GetMapping(value = "/warning_record")
    public Result<List<CamelliaEnvWarning>> getWarningRecord(@RequestParam(name="camelliaSysCode") String camelliaSysCode) {
        LocalDateTime localDateTime = LocalDateTime.now();
        DeviceWarningStatistical deviceWarningStatistical = new DeviceWarningStatistical();
        QueryWrapper<CamelliaEnvWarning> wrapper = new QueryWrapper<CamelliaEnvWarning>().orderByDesc("warning_date_time").eq("system_code",camelliaSysCode);
        wrapper.last("limit 100");
        List<CamelliaEnvWarning> list = camelliaEnvWarningService.list(wrapper);
        return Result.OK(list);
    }



}
